package com.xdz.controller;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xdz.entity.AttrEntity;
import com.xdz.entity.vo.AttrgroupRelationVo;
import com.xdz.entity.vo.GroupAndAttrsVo;
import com.xdz.service.CategoryService;
import com.xdz.utils.PageUtils;
import com.xdz.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xdz.entity.AttrGroupEntity;
import com.xdz.service.AttrGroupService;



/**
 * 属性分组
 *
 * @author xdz
 * @email 1351959729@qq.com
 * @date 2020-12-08 20:30:45
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long categoryId){
        PageUtils page = attrGroupService.queryPageByCategoryId(params,categoryId);

        return R.ok().put("page", page);
    }

    // 获取分组关联属性
    @GetMapping("/{id}/attr/relation")
    public R getGroupAttr(@PathVariable("id") Long id){
        List<AttrEntity> list = attrGroupService.getGroupAttr(id);
        return R.ok().put("data", list);
    }
    /**
     * 获取分组没有关联的属性
     */
    @RequestMapping("/{groupId}/noattr/relation")
    public R getGroupNoAttr(@RequestParam Map<String, Object> params,@PathVariable("groupId") Long groupId){
        PageUtils page = attrGroupService.getGroupNoAttr(params,groupId);
        return R.ok().put("page", page);
    }

    /**
     * 根据分类id获取属性分组和属性列表
     */
    @RequestMapping("/{categoryId}/withattr")
    public R getGroupAndAttrs(@PathVariable("categoryId") Long categoryId){
        List<GroupAndAttrsVo> vos = attrGroupService.getGroupAndAttrs(categoryId);
        return R.ok().put("data",vos);
    }


    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrgroupRelationVo> vos){
        attrGroupService.addAttrRelation(vos);
        return R.ok();
    }

    // 删除分组关联属性
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody List<AttrgroupRelationVo> vos){
        attrGroupService.attrRelationDelete(vos);
        return R.ok();
    }





    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long[] path = categoryService.selectCatelogPath(attrGroup.getCatelogId());
		attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
